//package com.pet.filter;
//
//import cn.hutool.core.collection.CollectionUtil;
//import cn.hutool.extra.spring.SpringUtil;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.pet.entity.UserDO;
//import com.pet.mapper.UserMapper;
//import com.pet.utils.UserContextUtil;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.Objects;
//
////实现过滤器 实现令牌的续期
//@WebFilter("/**")
//@Component
//public class AuthFilter implements Filter {
//
//    private static final String HEADER_TOKEN = "token";
//
//    @Override
//    public void init(FilterConfig filterConfig) {
//
//    }
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        //1.获取请求的令牌
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
//        if (CollectionUtil.newArrayList("/login", "/registerCheck", "/register").contains(path)) {
//            filterChain.doFilter(request,response);
//        } else {
//            String token = request.getHeader(HEADER_TOKEN);
//            UserMapper userMapper = SpringUtil.getBean(UserMapper.class);
//            LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.<UserDO>lambdaQuery()
//                    .eq(UserDO::getToken, token);
//            UserDO userDO = userMapper.selectOne(queryWrapper);
//            if (Objects.isNull(userDO)) {
//                response.setContentType("application/json;charset=utf-8");
//                response.setStatus(401);
//                response.getWriter().write("请先登录");
//            } else {
//                UserContextUtil.addCurrentUser(userDO);
//                //5.放行
//                filterChain.doFilter(request,response);
//            }
//        }
//    }
//
//    @Override
//    public void destroy() {
//    }
//}
